package com.yonyou.iuap.jxzy.servlet;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.yonyou.iuap.i18n.ApplicationContextUtil;


@Component
public class ApplicationContextHolder implements ApplicationContextAware{

    

   

    private static ApplicationContext applicationContext;

    @SuppressWarnings("all")

    public void setApplicationContext(ApplicationContext context) throws BeansException {

        if(this.applicationContext != null) {

            throw new IllegalStateException("ApplicationContextHolder already holded 'applicationContext'.");

        }

        this.applicationContext = context;



    }

    

    public static ApplicationContext getApplicationContext() {

        if(applicationContext == null)

            throw new IllegalStateException("'applicationContext' property is null,ApplicationContextHolder not yet init.");

        return applicationContext;

    }

    

    public static Object getBean(String beanName) {

        return getApplicationContext().getBean(beanName);

    }

    
    public static <T> T getBeanByClass(Class<T> c){
    	applicationContext=ApplicationContextUtil.getApplicationContext();
    	checkApplicationContext();
    	return (T)applicationContext.getBean(c);
    }
    
    private static void checkApplicationContext() {
		if (applicationContext == null) {
			throw new IllegalStateException("applicaitonContext未注入");
		}
	}

    

    public static void cleanHolder() {

        applicationContext = null;

    }

}